import com.sun.media.jfxmediaimpl.HostUtils;

import java.util.Arrays;

public class Sort {
    public static void mergeSort(int[] array) {
        mergeSortFun(array, 0, array.length - 1);
    }

    private static void mergeSortFun(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int mid = (start + end) / 2;
        mergeSortFun(array, start, mid);
        mergeSortFun(array, mid + 1, end);
        merge(array, start, mid, end);
    }

    private static void merge(int[] array, int left, int mid, int right) {
        int[] tmp = new int[right - left + 1];
        int k = 0;
        int s1 = left, s2 = mid + 1;
        int e1 = mid, e2 = right;
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] < array[s2]) {
                tmp[k++] = array[s1++];
            } else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[left + i] = tmp[i];
        }
    }

    //归并排序非递归
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i + gap * 2) {
                int left = i;
                int mid = left + gap - 1;
                int right = mid + gap;

                if (mid >= array.length) {
                    mid = array.length - 1;
                }
                if (right >= array.length) {
                    right = array.length - 1;
                }

                merge(array, left, mid, right);
            }
            gap *= 2;
        }
    }

    public static void countSort(int[] array) {
        int minVal = array[0];
        int maxVal = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] < minVal) {
                minVal = array[i];
            }
            if (array[i] > maxVal) {
                maxVal = array[i];
            }
        }

        int len = maxVal - minVal + 1;
        int[] count = new int[len];

        for (int i = 0; i < array.length; i++) {
            count[array[i] - minVal]++;
        }

        int index = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                array[index++] = i + minVal;
                count[i]--;
            }
        }
    }

    public static void insertSort(int[] array) {
        int len = array.length;
        for (int i = 1; i < len; i++) {
            int tmp = array[i];
            int j = 0;
            for (j = i - 1; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap = gap/3+1;
            for (int i = gap; i < array.length; i++) {
                int tmp = array[i];
                int j = 0;
                for (j = i - gap; j >= 0; j-=gap) {
                    if (array[j] > tmp) {
                        array[j + gap] = array[j];
                    } else {
                        break;
                    }
                }
                array[j + gap] = tmp;
            }
        }
    }

    public static void main(String[] args) {
        int[] array1 = {3, 9, 1, 5, 2, 6, 8, 4, 7};
        int[] array2 = {3, 9, 1, 5, 2, 6, 8, 4, 7};
        insertSort(array1);
        shellSort(array2);
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }
}
